package com.fhsk.file.common.utils.jedis;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.data.redis.serializer.SerializationException;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;
import java.io.*;
import java.nio.charset.Charset;
import java.util.*;

@Component
@Slf4j
public class JedisUtil {

    @Resource
    JedisPool jedisPool;


//-----------String Start  ------------------

    /**
     * String类型的增加操作
     *
     * @param key
     * @param value
     */
    public void set(String key, String value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.set(key, value);
        } catch (Exception e) {
            log.error("set error.", e);
            jedisPool.returnBrokenResource(jedis);
        } finally {
            jedisPool.returnResource(jedis);
        }
    }

    /**
     * 字符串
     *
     * @param key
     * @param value
     * @param expireTimeInSec
     */
    public void set(String key, String value, int expireTimeInSec) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.setex(key, expireTimeInSec, value);
        } catch (Exception e) {
            log.error("set ex error.", e);
            jedisPool.returnBrokenResource(jedis);
        } finally {
            jedisPool.returnResource(jedis);
        }
    }

    public String get(String key) {
        Jedis jedis = null;
        String value = null;
        try {
            jedis = jedisPool.getResource();
            value = jedis.get(key);
        } catch (Exception e) {
            log.error("getkey error.", e);
            jedisPool.returnBrokenResource(jedis);
        } finally {
            jedisPool.returnResource(jedis);
        }
        return value;
    }

    /**
     * 获取全量key
     *
     * @param key
     * @return
     */
    public Set<String> getAll(String key) {
        Jedis jedis = null;
        Set<String> keys = null;
        try {
            jedis = jedisPool.getResource();
            keys = jedis.keys(key + "*");
        } catch (Exception e) {
            log.error("getAllkey error.", e);
            jedisPool.returnBrokenResource(jedis);
        } finally {
            jedisPool.returnResource(jedis);
        }
        return keys;
    }

    /**
     * 删除全量key
     *
     * @param key
     * @return
     */
    public Long delAll(String key) {
        Jedis jedis = null;
        Set<String> keys = null;
        try {
            jedis = jedisPool.getResource();
            keys = jedis.keys(key + "*");
            Iterator<String> it = keys.iterator();
            while (it.hasNext()) {
                String keyStr = it.next();
                jedis.del(keyStr);
            }
        } catch (Exception e) {
            log.error("delAll error.", e);
            jedisPool.returnBrokenResource(jedis);
        } finally {
            jedisPool.returnResource(jedis);
        }
        return 0L;
    }

    /**
     * 删除某个key
     *
     * @param key
     */
    public Long del(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.del(key);
        } catch (Exception e) {
            log.error("pop ", e);
            jedisPool.returnBrokenResource(jedis);
        } finally {
            jedisPool.returnResource(jedis);
        }
        return 0L;
    }

    /**
     * 存入对象之前进行序列化
     *
     * @param key   键
     * @param value 值
     * @return
     */
    public String setObj(String key, Object value) {
        String result = null;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            result = jedis.set(key.getBytes(), serialize(value));

            log.debug("setObject {} = {}", key, value);
        } catch (Exception e) {
            log.warn("setObject {} = {}", key, value, e);
        } finally {
            jedisPool.returnResource(jedis);
        }
        return result;
    }

    /**
     * 获取List
     *
     * @param key 键
     * @return
     */
    public <T> List<T> getList(String key, Class<T> clazz) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            byte[] bytes = jedis.get(key.getBytes());
            if (null != bytes && bytes.length > 0) {
                String str = new String(bytes, DEFAULT_CHARSET);
                return JSON.parseArray(str, clazz);
            }
            return null;
        } catch (Exception e) {
            log.error("getList error.", e);
            jedisPool.returnBrokenResource(jedis);
            return null;
        } finally {
            jedisPool.returnResource(jedis);
        }
    }

    /**
     * 设置缓存
     *
     * @param key          键
     * @param value        值
     * @param cacheSeconds 超时时间，0为不超时
     * @return
     */
    public String setObj(String key, Object value, int cacheSeconds) {
        String result = null;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            result = jedis.set(key.getBytes(), serialize(value));
            if (cacheSeconds != 0) {
                jedis.expire(key, cacheSeconds);
            }
            log.debug("setObject {} = {}", key, value);
        } catch (Exception e) {
            log.warn("setObject {} = {}", key, value, e);
        } finally {
            jedisPool.returnResource(jedis);
        }
        return result;
    }

    /**
     * getObj
     *
     * @param key 键
     * @return
     */
    public <T> T getObj(String key, Class<T> clazz) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            byte[] bytes = jedis.get(key.getBytes());
            if (null != bytes && bytes.length > 0) {
                return deserialize(bytes, clazz);
            }

            return null;
        } catch (Exception e) {
            log.error("getobj error.", e);
            jedisPool.returnBrokenResource(jedis);
            return null;
        } finally {
            jedisPool.returnResource(jedis);
        }
    }

    /**
     * 删除key
     */
    public Long delObj(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.del(key.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }


    /**
     * 判断某个key值是否存在
     *
     * @param key
     * @return
     */
    public Boolean exists(String key) {
        Jedis jedis = null;
        Boolean flag = false;

        try {
            jedis = jedisPool.getResource();
            flag = jedis.exists(key);
        } catch (Exception e) {
            log.error("set error.", e);
            jedisPool.returnBrokenResource(jedis);
        } finally {
            jedisPool.returnResource(jedis);
        }
        return flag;
    }

    //-----------String End  ------------------
    public boolean setNX(String key, String value) {
        Jedis jedis = null;
        Long setnx = null;
        try {
            jedis = jedisPool.getResource();
            setnx = jedis.setnx(key, value);
        } catch (Exception e) {
            log.error("setNX error.", e);
        } finally {
            jedis.close();
        }
        return setnx.equals(1L);
    }

    public Long decr(String key) {
        Jedis jedis = null;
        Long value = null;
        try {
            jedis = jedisPool.getResource();
            value = jedis.decr(key);
        } catch (Exception e) {
            log.error("getkey error.", e);
            jedisPool.returnBrokenResource(jedis);
        } finally {
            jedisPool.returnResource(jedis);
        }
        return value;
    }


//----------Set Start  ------------------

    /**
     * 向Set缓存中添加值
     *
     * @param key   键
     * @param value 值
     * @return
     */
    public long setSetAdd(String key, String... value) {
        long result = 0;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            result = jedis.sadd(key, value);
            log.debug("setSetAdd {} = {}", key, value);
        } catch (Exception e) {
            log.warn("setSetAdd {} = {}", key, value, e);
        } finally {
            jedisPool.returnResource(jedis);
        }
        return result;
    }

    /**
     * 设置Set缓存
     *
     * @param key          键
     * @param value        值
     * @param cacheSeconds 超时时间，0为不超时
     * @return
     */
    public long setSet(String key, Set<String> value, int cacheSeconds) {
        long result = 0;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            if (jedis.exists(key)) {
                jedis.del(key);
            }
            result = jedis.sadd(key, value.toArray(new String[value.size()]));
            if (cacheSeconds != 0) {
                jedis.expire(key, cacheSeconds);
            }
            log.debug("setSet {} = {}", key, value);
        } catch (Exception e) {
            log.warn("setSet {} = {}", key, value, e);
        } finally {
            jedisPool.returnResource(jedis);
        }
        return result;
    }

    /**
     * 获取缓存
     *
     * @param key 键
     * @return 值
     */
    public Set<String> getSet(String key) {
        Set<String> value = null;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            if (jedis.exists(key)) {
                value = jedis.smembers(key);
                log.debug("getSet {} = {}", key, value);
            }
        } catch (Exception e) {
            log.warn("getSet {} = {}", key, value, e);
        } finally {
            jedisPool.returnResource(jedis);
        }
        return value;
    }

    /**
     * 设置Set缓存
     *
     * @param key          键
     * @param value        值
     * @param cacheSeconds 超时时间，0为不超时
     * @return
     */
    public long setObjectSet(String key, Set<Object> value, int cacheSeconds) {
        long result = 0;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            if (jedis.exists(key.getBytes())) {
                jedis.del(key);
            }
            Set<byte[]> set = new HashSet();
            for (Object o : value) {
                set.add(serialize(o));
            }
            result = jedis.sadd(key.getBytes(), set.toArray(new byte[set.size()][]));
            if (cacheSeconds != 0) {
                jedis.expire(key, cacheSeconds);
            }
            log.debug("setObjectSet {} = {}", key, value);
        } catch (Exception e) {
            log.warn("setObjectSet {} = {}", key, value, e);
        } finally {
            jedisPool.returnResource(jedis);
        }
        return result;
    }

    /**
     * 获取缓存
     *
     * @param key 键
     * @return 值
     */
    public Set<T> getObjectSet(String key, Class<T> clazz) {
        Set<T> value = null;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            if (jedis.exists(key.getBytes())) {
                value = new HashSet();
                Set<byte[]> set = jedis.smembers(key.getBytes());
                for (byte[] bs : set) {
                    value.add(deserialize(bs, clazz));
                }
                log.debug("getObjectSet {} = {}", key, value);
            }
        } catch (Exception e) {
            log.warn("getObjectSet {} = {}", key, value, e);
        } finally {
            jedisPool.returnResource(jedis);
        }
        return value;
    }


    /**
     * 向Set缓存中添加值
     *
     * @param key   键
     * @param value 值
     * @return
     */
    public long setSetObjectAdd(String key, Object... value) {
        long result = 0;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            Set<byte[]> set = new HashSet();
            for (Object o : value) {
                //防止传数组，有null报错
                if (o == null) {
                    continue;
                }
                set.add(serialize(o));

            }
            result = jedis.sadd(key.getBytes(), set.toArray(new byte[set.size()][]));
            log.debug("setSetObjectAdd {} = {}", key, value);
        } catch (Exception e) {
            log.warn("setSetObjectAdd {} = {}", key, value, e);
        } finally {
            jedisPool.returnResource(jedis);
        }
        return result;
    }

    /**
     * 获取给定key中元素个数
     *
     * @param key
     * @return
     */
    public long scard(String key) {
        Jedis jedis = null;
        long len = 0;
        try {
            jedis = jedisPool.getResource();
            len = jedis.scard(key);
        } catch (Exception e) {
            log.warn("scard {} = {}", key, e);
        } finally {
            jedisPool.returnResource(jedis);
        }
        return len;
    }

    /**
     * 判断KEY关联的SET集合是否存在对应的成员
     *
     * @param key   Redis里面实际的KEY
     * @param value 要查找的成员
     */
    public boolean sismember(String key, String value) {
        Jedis jedis = null;
        boolean flag = false;
        try {
            jedis = jedisPool.getResource();
            flag = jedis.sismember(key, value);
        } catch (Exception e) {
            log.error("set cache error.", e);
            jedisPool.returnBrokenResource(jedis);
        } finally {
            jedisPool.returnResource(jedis);
        }
        return flag;
    }

    /**
     * 从集合中删除指定成员
     *
     * @param key
     * @param value
     * @return
     */
    public long srem(String key, String value) {
        Jedis jedis = null;
        long flag = 0;
        try {
            jedis = jedisPool.getResource();
            flag = jedis.srem(key, value);
        } catch (Exception e) {
            log.error("set cache error.", e);
            jedisPool.returnBrokenResource(jedis);
        } finally {
            jedisPool.returnResource(jedis);
        }
        return flag;
    }

    //-----------Set End  --------------------


    //-----------List Start-------------------

    /**
     * List放入多个对象
     *
     * @param name
     * @param ts
     * @param <T>
     */
    public <T extends Serializable> void push(String name, T... ts) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            for (T t : ts) {
                jedis.lpush(name, JSON.toJSONString(t));
            }
        } catch (Exception e) {
            log.error("push error.", e);
            jedisPool.returnBrokenResource(jedis);
        } finally {
            jedisPool.returnResource(jedis);
        }
    }

    /**
     * List放入单个值
     *
     * @param name
     * @param json
     * @return
     */
    public long lpush(String name, String json) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.lpush(name, json);
        } catch (Exception e) {
            log.error("push error.", e);
            jedisPool.returnBrokenResource(jedis);
        } finally {
            jedisPool.returnResource(jedis);
        }
        return -1;
    }

    /**
     * List放入集合
     *
     * @param name
     * @param collection
     * @param <T>
     */
    public <T extends Serializable> void push(String name, Collection<T> collection) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            for (T t : collection) {
                jedis.lpush(name, JSON.toJSONString(t));
            }
        } catch (Exception e) {
            log.error("push collection error.", e);
            jedisPool.returnBrokenResource(jedis);
        } finally {
            jedisPool.returnResource(jedis);
        }
    }

    /**
     * 从列表最后一位开始移除并获取列表该元素
     *
     * @param name
     * @param size
     * @param <T>
     * @return
     */
    @SuppressWarnings("unchecked")
    public <T extends Serializable> List<T> pop(String name, int size) {
        if (size < 1) {
            return null;
        }

        Jedis jedis = null;
        List<T> list = new ArrayList<T>();
        try {
            jedis = jedisPool.getResource();
            if (jedis.exists(name)) {
                String value = jedis.rpop(name);
                for (int i = 1; i <= size && value != null; i++) {
                    list.add((T) JedisUtil.str2Object(value));
                    value = jedis.rpop(name);
                }
            }
        } catch (Exception e) {
            log.error("pop ", e);
            jedisPool.returnBrokenResource(jedis);
        } finally {
            jedisPool.returnResource(jedis);
        }

        return list;
    }

    public <T extends Serializable> List<T> lrange(String name, int start, int end) {
        Jedis jedis = null;
        List<T> list = new ArrayList<T>();
        try {
            jedis = jedisPool.getResource();
            if (jedis.exists(name)) {
                List<String> strList = jedis.lrange(name, start, end);
                int size = strList == null ? 0 : strList.size();
                for (int i = 0; i < size; i++) {
                    list.add((T) JedisUtil.str2Object(strList.get(i)));
                }
            }
        } catch (Exception e) {
            log.error("lrange ", e);
            jedisPool.returnBrokenResource(jedis);
        } finally {
            jedisPool.returnResource(jedis);
        }

        return list;
    }

    public <T extends Serializable> List<T> lrange(String name, int start, int end, Class<T> classes) {
        Jedis jedis = null;
        List<T> list = new ArrayList<T>();
        try {
            jedis = jedisPool.getResource();
            if (jedis.exists(name)) {
                List<String> strList = jedis.lrange(name, start, end);
                int size = strList == null ? 0 : strList.size();
                ObjectMapper objectMapper = new ObjectMapper();
                for (int i = 0; i < size; i++) {
                    list.add(objectMapper.readValue(strList.get(i), classes));
                }
            }
        } catch (Exception e) {
            log.error("lrange ", e);
            jedisPool.returnBrokenResource(jedis);
        } finally {
            jedisPool.returnResource(jedis);
        }

        return list;
    }

    public <T extends Serializable> List<T> rrange(String name, int start, int end) {
        Jedis jedis = null;
        List<T> list = new ArrayList<T>();
        try {
            jedis = jedisPool.getResource();
            if (jedis.exists(name)) {
                List<String> strList = jedis.lrange(name, start, end);
                int size = strList == null ? 0 : strList.size();
                for (int i = size - 1; i >= 0; i--) {
                    list.add((T) JedisUtil.str2Object(strList.get(i)));
                }
            }
        } catch (Exception e) {
            log.error("lrange ", e);
            jedisPool.returnBrokenResource(jedis);
        } finally {
            jedisPool.returnResource(jedis);
        }

        return list;
    }

    public <T extends Serializable> List<T> rrange(String name, int start, int end, Class<T> classes) {
        Jedis jedis = null;
        List<T> list = new ArrayList<T>();
        try {
            jedis = jedisPool.getResource();
            if (jedis.exists(name)) {
                List<String> strList = jedis.lrange(name, start, end);
                int size = strList == null ? 0 : strList.size();
                ObjectMapper objectMapper = new ObjectMapper();
                for (int i = size - 1; i >= 0; i--) {
                    list.add(objectMapper.readValue(strList.get(i), classes));
                }
            }
        } catch (Exception e) {
            log.error("lrange ", e);
            jedisPool.returnBrokenResource(jedis);
        } finally {
            jedisPool.returnResource(jedis);
        }

        return list;
    }


    public long length(String name) {
        Jedis jedis = null;
        long length = 0;
        try {
            jedis = jedisPool.getResource();
            length = jedis.llen(name);
        } catch (Exception e) {
            log.error("pop ", e);
            jedisPool.returnBrokenResource(jedis);
        } finally {
            jedisPool.returnResource(jedis);
        }

        return length;
    }

    /**
     * 移除列表中的元素
     *
     * @param name
     * @param count count > 0 : 从表头开始向表尾搜索，移除与 VALUE 相等的元素，数量为 COUNT 。
     *              count < 0 : 从表尾开始向表头搜索，移除与 VALUE 相等的元素，数量为 COUNT 的绝对值。
     *              count = 0 : 移除表中所有与 VALUE 相等的值。
     */
    public void lrem(String name, int count, String value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.lrem(name, count, JSON.toJSONString(value));
        } catch (Exception e) {
            log.error("pop ", e);
            jedisPool.returnBrokenResource(jedis);
        } finally {
            jedisPool.returnResource(jedis);
        }
    }
//---------------List End----------------
//--------------ZSet Start---------------

    /**
     * 向有序不重复集合添加数据
     *
     * @param key
     * @param score
     * @param member
     * @return
     */
    public long zadd(String key, double score, String member) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.zadd(key, score, member);
        } catch (Exception e) {
            log.error("zadd ", e);
            jedisPool.returnBrokenResource(jedis);
        } finally {
            jedisPool.returnResource(jedis);
        }
        return 0;
    }

    public long zadd(String key, Map<String, Double> scoreMembers) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.zadd(key, scoreMembers);
        } catch (Exception e) {
            log.error("zadd ", e);
            jedisPool.returnBrokenResource(jedis);
        } finally {
            jedisPool.returnResource(jedis);
        }
        return 0;
    }

    /**
     * 移除有序集合中给定的字典区间的所有成员
     *
     * @param key
     * @param start
     * @param end
     * @return
     */
    public long zremrangeByRank(String key, long start, long end) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.zremrangeByRank(key, start, end);
        } catch (Exception e) {
            log.error("zremrangeByRank ", e);
            jedisPool.returnBrokenResource(jedis);
        } finally {
            jedisPool.returnResource(jedis);
        }
        return 0;
    }

    /**
     * 通过索引区间返回有序集合成指定区间内的成员
     *
     * @param key
     * @param start
     * @param end
     * @return
     */
    public Set<String> zrange(String key, long start, long end) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.zrange(key, start, end);
        } catch (Exception e) {
            log.error("zrang ", e);
            jedisPool.returnBrokenResource(jedis);
        } finally {
            jedisPool.returnResource(jedis);
        }
        return null;
    }

    /**
     * 获取有序集合的成员数
     *
     * @param key
     * @return
     */
    public long zcard(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.zcard(key);
        } catch (Exception e) {
            log.error("zcard ", e);
            jedisPool.returnBrokenResource(jedis);
        } finally {
            jedisPool.returnResource(jedis);
        }
        return 0;
    }
//--------------End ZSet-----------------------
//--------------Start Hash---------------------

    /**
     * 将哈希表 key 中的字段 field 的值设为 value
     *
     * @param key
     * @param field
     * @param value
     * @return
     */
    public long hset(String key, String field, String value) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.hset(key, field, value);
        } catch (Exception e) {
            log.error("hset ", e);
            jedisPool.returnBrokenResource(jedis);
        } finally {
            jedisPool.returnResource(jedis);
        }
        return 0L;
    }

    public long hsetObj(String key, String field, Object value) {
        return hset(key, field, new String(serialize(value)));
    }

    /**
     * 获取所有给定字段的值
     *
     * @param key
     * @param field
     * @return
     */
    public String hget(String key, String field) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.hget(key, field);
        } catch (Exception e) {
            log.error("hget ", e);
            jedisPool.returnBrokenResource(jedis);
        } finally {
            jedisPool.returnResource(jedis);
        }
        return null;
    }

    public <T> T hgetObj(String key, String field, Class<T> clazz) {
        String obj = hget(key, field);
        if (obj != null) {
            return deserialize(obj.getBytes(), clazz);
        }
        return null;
    }

    public <T> List<T> hgetObjList(String key, String field, Class<T> clazz) {
        String obj = hget(key, field);
        if (obj != null) {
            return deserializeList(obj.getBytes(), clazz);
        }
        return null;
    }

    /**
     * 获取大key下的所有键值对数据
     *
     * @param key
     * @return
     */
    public Map<String, String> hgetAll(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.hgetAll(key);
        } catch (Exception e) {
            log.error("hgetAll ", e);
            jedisPool.returnBrokenResource(jedis);
        } finally {
            jedisPool.returnResource(jedis);
        }
        return null;
    }

    /**
     * 获取当前key的长度
     *
     * @param key
     * @return
     */
    public Long hlen(String key) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.hlen(key);
        } catch (Exception e) {
            log.error("hcard ", e);
            jedisPool.returnBrokenResource(jedis);
        } finally {
            jedisPool.returnResource(jedis);
        }
        return 0L;
    }

    /**
     * 删除一个或多个哈希表字段
     *
     * @param key
     * @param field
     * @return
     */
    public Long hdel(String key, String field) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return jedis.hdel(key, field);
        } catch (Exception e) {
            log.error("hget ", e);
            jedisPool.returnBrokenResource(jedis);
        } finally {
            jedisPool.returnResource(jedis);
        }
        return null;
    }


    public static Object str2Object(String str) throws Exception {

        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(getByte(str)));
        return ois.readObject();
    }

    public static byte[] getByte(String str) {
        byte[] bt = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            if (str != null) {
                ObjectOutputStream objos = new ObjectOutputStream(baos);
                objos.writeObject(str);
                bt = baos.toByteArray();
            }
        } catch (Exception e) {
            bt = null;
            e.printStackTrace();
        }
        return bt;
    }

    public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");

    public static byte[] serialize(Object object) throws SerializationException {
        if (object == null) {
            return new byte[0];
        }
        String s = JSONObject.toJSONString(object);
        return JSONObject.toJSONString(object).getBytes(DEFAULT_CHARSET);
    }

    public static <T> T deserialize(byte[] bytes, Class<T> clazz) throws SerializationException {
        if (bytes == null || bytes.length <= 0) {
            return null;
        }
        String str = new String(bytes, DEFAULT_CHARSET);
        return JSONObject.parseObject(str, clazz);
    }

    public static <T> List<T> deserializeList(byte[] bytes, Class<T> clazz) throws SerializationException {
        if (bytes == null || bytes.length <= 0) {
            return null;
        }
        String str = new String(bytes, DEFAULT_CHARSET);
        return JSONObject.parseArray(str, clazz);
    }
}
